#include "BufferWriter.h"
#include <stdio.h>
#include <string.h>

using std::string;

BufferWriter::BufferWriter() {
	pos = 0;
	memset (buffer, 0, 65536);

};

BufferWriter::~BufferWriter() {
	pos = 0;
}

void BufferWriter::addByte(int8_t byte) {
	buffer[pos] = byte;
	pos++;
}

void BufferWriter::setByte(int8_t byte, int32_t pos) {
	buffer[pos] = byte;
}

void BufferWriter::addShort(int16_t shrt) {
	(*(int16_t*)(buffer+pos)) = shrt;
	pos += 2;
}

void BufferWriter::setShort(int16_t shrt, int32_t pos) {
	(*(int16_t*)(buffer+pos)) = shrt;
}

void BufferWriter::addInt(int32_t intg) {
	(*(int32_t*)(buffer+pos)) = intg;
	pos += 4;
}

void BufferWriter::setInt(int32_t intg, int32_t pos) {
	(*(int32_t*)(buffer+pos)) = intg;
}

void BufferWriter::addLong(int64_t lng) {
	(*(int64_t*)(buffer+pos)) = lng;
	pos += 8;
}

void BufferWriter::setLong(int64_t lng, int32_t pos) {
	(*(int64_t*)(buffer+pos)) = lng;
}

void BufferWriter::addString(const string &stri) {
	int16_t len = stri.size();
	addShort(len);
	strncpy((char *)buffer + pos, stri.c_str(), len);
	//strcpy_s((char *) buffer + pos, 10000-pos, stri.c_str());
	pos += len;
}

void BufferWriter::addDouble(double dbl) {
	(*(double *)(buffer + pos)) = dbl;
	pos += 8;
}

void BufferWriter::setDouble(double dbl, int32_t pos) {
	(*(double *)(buffer + pos)) = dbl;
}

uint8_t *BufferWriter::getBuffer() {
	return buffer;
}

uint32_t BufferWriter::getPos() {
	return pos;
}
